const Model = require('./index')

class ToolModel extends Model {
  /**
   * 创建工具分类
   * @param {*} attrs
   */
  async createToolClassify (attrs) {
    const createTime = this.dateFormat(new Date())
    const { name, icon } = attrs
    const sql = 'insert into tool_classify (name, icon, createTime) values(?, ?, ?)'
    try {
      await this.db.query(sql, [name, icon, createTime])
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 工具分类列表查询
   */
  async findToolClassify () {
    const sql = 'select tc.*, (select count(*) from tool where tc.id = tool.classifyId) as toolCount from tool_classify tc'
    try {
      const data = await this.db.query(sql)
      return data
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 工具添加
   * @param {*} attrs
   */
  async createTool (attrs) {
    const createTime = this.dateFormat(new Date())
    const { name, description, logo, url, classifyId } = attrs
    const sql = `insert into tool (name, description, logo, url, classifyId, createTime)
      values(?, ?, ?, ?, ?, ?)`
    try {
      await this.db.query(sql, [name, description, logo, url, classifyId, createTime])
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 工具列表查询
   * @returns
   */
  async findTools (conditions, userId) {
    const completeSql = this.generateWhereClause(conditions, userId)
    const sql = `select tool.*, (select userId from user_collect_tool uct where uct.userId = ? and tool.id = uct.toolId) as collected, 
      tc.name as classifyName, tc.icon, tc.id as classifyId from tool
      left join tool_classify tc on tc.id = tool.classifyId ${completeSql} 
      order by tool.classifyId`
    try {
      const data = await this.db.query(sql, [userId])
      return data
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 工具分类批量删除
   * @param {*} ids
   */
  async deleteToolClassifys (ids) {
    const sql = 'delete from tool_classify where id in (?)'
    try {
      await this.db.query(sql, ids.join(','))
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 工具批量删除
   * @param {*} ids
   */
  async deleteTools (ids) {
    const sql = 'delete from tool where id in (?)'
    try {
      await this.db.query(sql, [ids])
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 工具分类编辑
   * @param {*} id
   * @param {*} attrs
   */
  async updateToolClassify (id, attrs) {
    const updateTime = this.dateFormat(new Date())
    const { name, icon } = attrs
    const { columnSet, values } = this.multipleColumnSet({
      name,
      icon,
      updateTime
    })
    const sql = `update tool_classify set ${columnSet} where id = ?`
    try {
      await this.db.query(sql, [...values, id])
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 工具编辑
   * @param {*} id
   * @param {*} attrs
   */
  async updateTool (id, attrs) {
    const updateTime = this.dateFormat(new Date())
    const { name, logo, classifyId, desc, url } = attrs
    const { columnSet, values } = this.multipleColumnSet({
      name,
      logo,
      classifyId,
      url,
      description: desc,
      updateTime
    })
    const sql = `update tool set ${columnSet} where id = ?`
    try {
      await this.db.query(sql, [...values, id])
    } catch (error) {
      throw new Error(error)
    }
  }

  async isExistClassify (id) {
    const sql = 'select id from tool_classify where id = ?'
    try {
      const data = await this.db.query(sql, [id])
      return Boolean(data[0])
    } catch (error) {
      throw new Error(error)
    }
  }

  async isExistTool (id) {
    const sql = 'select id from tool where id = ?'
    try {
      const data = await this.db.query(sql, [id])
      return Boolean(data[0])
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 查询用户收藏的工具列表
   * @param {*} userId
   */
  async findCollectTool (userId) {
    const sql = `select tool.* from tool where tool.id in (
      select toolId from user_collect_tool where userId = ?
    )`
    try {
      const data = await this.db.query(sql, [userId])
      return data
    } catch (error) {
      throw new Error(error)
    }
  }
}

module.exports = new ToolModel()
